package com.lenovo.leos.filebrowser.engine.model;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Comparator;

import android.content.Context;

import com.lenovo.leos.filebrowser.engine.util.HzToPy;

public abstract class FileComparator implements Comparator<IconifiedText> {
	protected Context mContext = null;
	private static HzToPy mHz2Py;

	public FileComparator(Context context) throws IOException {
		mContext = context;
		if (mHz2Py == null) {
			mHz2Py = new HzToPy(mContext);
		}
	}

	public int compareString(String str1, String str2)
			throws UnsupportedEncodingException {
		if (null == str1 && null == str2)
			return 0;
		else if (null == str1 && null != str2)
			return -1;
		else if (null != str1 && null == str2)
			return 1;
		else {
			int len1 = str1.length();
			int len2 = str2.length();
			if (0 == len1 && 0 == len2)
				return 0;
			else if (0 != len1 && 0 == len2)
				return 1;
			else if (0 == len1 && 0 != len2)
				return -1;
			else {
				int max = len1 > len2 ? len1 : len2;
				int i = 0;
				while (i < max) {
					if (i == len1 && i < len2)
						return -1;
					else if (i < len1 && i == len2)
						return 1;
					else {
						char c1 = str1.charAt(i);
						char c2 = str2.charAt(i);
						if (c1 != c2) {
							if (HzToPy.isHzChar(c1) && HzToPy.isHzChar(c2)) {
								String hz1 = HzToPy.Char2Py(c1);
								String hz2 = HzToPy.Char2Py(c2);
								int res = hz1.compareTo(hz2);
								if (0 == res) {
									return c1 > c2 ? 1 : -1;
								} else
									return res;
							} else
								return c1 > c2 ? 1 : -1;
						}
					}

					i++;
				}
				return 0;
			}
		}
	}
}